/*****************************************************************************
	SNEGA UTILITIES 0.7a
	Copyright (C) 2009, Xiaohan Tian (snega0223)
	
	http://code.google.com/p/snega-utilities/
	
	This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License(LGPL) as
	published by the Free Software Foundation, either version 3 of the 
	License, or(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU Lesser General Public License for more details.
	
	You should have received a copy of the GNU Lesser General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*****************************************************************************/

package org.sng.util.db.rawsql;

import org.sng.util.db.*;
import org.sng.util.db.elmt.*;
import org.sng.util.db.rawsql.cond.*;
import org.sng.util.db.rawsql.order.*;
import org.sng.util.db.rawsql.target.*;

public class SQLSelect extends RawSQL {
	
	protected Order m_order;
	
	protected int m_from;
	protected int m_amount;
	
	protected SQLSelect()
	{
		this.m_order = new Order();
		
		this.m_from = 0;
		this.m_amount = -1;
	}
	
	protected SQLSelect(String s)
	{
		super(s);
		
		this.m_order = new Order();
		
		this.m_from = 0;
		this.m_amount = -1;
	}
	
	public DbConn Execute()
	{
		return this.m_dbo.executeQuery(this.ToSQL());
	}
	
	public DataTable Get()
	{
		DataTable table = new DataTable(this);
		
		table.Refresh();
		
		return table;
	}
	
	/* ------ Output ------ */
	public String ToSQL()
	{
		StringBuffer sb = new StringBuffer();
		
		sb.append("select * from ");
		
		sb.append(this.m_target.AllToString());
		
		sb.append(" ");
		
		sb.append(this.m_cond.AllToString());
		
		sb.append(" ");
		
		sb.append(this.m_order.AllToString());
		
		if (this.m_amount != -1)
		{
			sb.append(" ");
			sb.append(this.m_dbo.BuildLimit(this.m_from, this.m_amount));
		}
		
		return sb.toString();
	}
	
	/* ------ Additional Target ------ */
	public SQLSelect AppendTarget(String s)
	{
		this.m_target.AppendTarget(new Target(s));
		
		return this;
	}
	
	/* ------ Additional Conditions ------ */
	public SQLSelect WhereEquare(String s, int n)
	{
		this.m_cond.AppendCondition(new ConditionForNumber(Compare.EQUARE, s, String.valueOf(n)));
		
		return this;
	}
	
	public SQLSelect WhereEquare(String s, double d)
	{
		this.m_cond.AppendCondition(new ConditionForNumber(Compare.EQUARE, s, String.valueOf(d)));
		
		return this;
	}
	
	public SQLSelect WhereEquare(String s, String compare)
	{
		this.m_cond.AppendCondition(new ConditionForString(Compare.EQUARE, s, compare));
		
		return this;
	}
	
	public SQLSelect WhereNotEquare(String s, int n)
	{
		this.m_cond.AppendCondition(new ConditionForNumber(Compare.NOTEQUARE, s, String.valueOf(n)));
		
		return this;
	}
	
	public SQLSelect WhereNotEquare(String s, double d)
	{
		this.m_cond.AppendCondition(new ConditionForNumber(Compare.NOTEQUARE, s, String.valueOf(d)));
		
		return this;
	}
	
	public SQLSelect WhereNotEquare(String s, String compare)
	{
		this.m_cond.AppendCondition(new ConditionForString(Compare.NOTEQUARE, s, compare));
		
		return this;
	}
	
	public SQLSelect WhereBigger(String s, int n)
	{
		this.m_cond.AppendCondition(new ConditionForNumber(Compare.BIGGER, s, String.valueOf(n)));
		
		return this;
	}
	
	public SQLSelect WhereBigger(String s, double d)
	{
		this.m_cond.AppendCondition(new ConditionForNumber(Compare.BIGGER, s, String.valueOf(d)));
		
		return this;
	}
	
	public SQLSelect WhereBigger(String s, String compare)
	{
		this.m_cond.AppendCondition(new ConditionForString(Compare.BIGGER, s, compare));
		
		return this;
	}
	
	public SQLSelect WhereSmaller(String s, int n)
	{
		this.m_cond.AppendCondition(new ConditionForNumber(Compare.SMALLER, s, String.valueOf(n)));
		
		return this;
	}
	
	public SQLSelect WhereSmaller(String s, double d)
	{
		this.m_cond.AppendCondition(new ConditionForNumber(Compare.SMALLER, s, String.valueOf(d)));
		
		return this;
	}
	
	public SQLSelect WhereSmaller(String s, String compare)
	{
		this.m_cond.AppendCondition(new ConditionForString(Compare.SMALLER, s, compare));
		
		return this;
	}
	
	/* ------ Additional Orders ------ */
	public SQLSelect OrderByDesc(String s)
	{
		this.m_order.AppendOrder(new OrderByDesc(s));
		
		return this;
	}
	
	public SQLSelect OrderByAsc(String s)
	{
		this.m_order.AppendOrder(new OrderByAsc(s));
		
		return this;
	}
	
	/* ------ Additional Limit ------ */
	public SQLSelect Limit(int n)
	{
		this.m_amount = n;
		
		return this;
	}
	
	public SQLSelect Limit(int m, int n)
	{
		this.m_from = m;
		this.m_amount = n;
		
		return this;
	}
	
	public SQLSelect PageCtrl(int pageNum, int amountPerPage)
	{
		this.m_from = amountPerPage * pageNum;
		this.m_amount = amountPerPage;
		
		return this;
	}

}
